﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Orion.Models
{
    public class NewsArticleRepositry : INewsArticleRepository
    {
		public OrionDBDataContext m_db = new OrionDBDataContext();

        public IQueryable<NewsArticle> GetAllNewsArticles()
		{
            var result = from na in m_db.NewsArticles
                          orderby na.ContentDate descending
                          select na;
            return result;
        }

        public NewsArticle GetNewsArticleById( int id )
        {
            var result = (from na in m_db.NewsArticles
                          where na.ID == id
                          select na).SingleOrDefault();
            return result;
        }

		public IQueryable<NewsArticle> GetAllNewsByName(string searchString)
		{
			var result = from na in m_db.NewsArticles
						where na.Title.Contains(searchString) ||
						na.TextBody.Contains(searchString)
						 orderby na.Title
						 select na;
			return result;
		}

        /*Creating and editing news or article*/

        public void AddNewsArticle( NewsArticle n )
        {
            m_db.NewsArticles.InsertOnSubmit(n);

            m_db.SubmitChanges();
        }

        public void GetAllCategories( NewsArticle na )
        {
            NewsArticle temp = GetNewsArticleById( na.ID );
            temp.Title = na.Title;
            temp.TextBody = na.TextBody;
            temp.ContentType = na.ContentType;
            temp.ContentDate = DateTime.Now;
            temp.Author = na.Author;
            temp.Picture = na.Picture;
            temp.Category = na.Category;
            temp.Counter = na.Counter;

            m_db.SubmitChanges();
        }

        public IQueryable<Category> GetAllCategories()
        {
            var result = from n in m_db.Categories
                         select n;
            return result;
        }

		public void DisableNewsArticle( NewsArticle na, String strUser )
		{
		    if (strUser == na.Author || HttpContext.Current.User.IsInRole("Admin") == true )
		    {
		        na.Valid = false;
		        m_db.SubmitChanges();
		    }
		}

		public void EnableNewsArticle(NewsArticle na, String strUser)
		{
		    if( strUser == na.Author || HttpContext.Current.User.IsInRole("Admin") == true )
		    {
		        na.Valid = true;
	            m_db.SubmitChanges();
		    }
		}

        public void UpdateNewsArticle( NewsArticle na )
        {
            NewsArticle temp = GetNewsArticleById(na.ID);

			if( ! string.IsNullOrEmpty( na.Picture ) )
			{
				temp.Picture = na.Picture;
			}
			temp.Author = na.Author;
            temp.Title = na.Title;
            temp.TextBody = na.TextBody;
            temp.ContentType = na.ContentType;
            temp.ContentDate = DateTime.Now;
            temp.Category = na.Category;
            temp.Counter = na.Counter;
            temp.Valid = na.Valid;

            m_db.SubmitChanges();
        }
    }
}